package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cc.comac.mom.bean.*;
import cc.comac.mom.bean.qar.QualityQarCheckInvoiceBean;
import cc.comac.mom.dao.entity.QualityAsrCheckFormDetailDao;
import cc.comac.mom.dao.entity.QualityQarBasicStandardAsrFeaturesDao;
import cc.comac.mom.dao.entity.QualityQarCheckInvoiceDao;
import cc.comac.mom.entity.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;

import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityAsrCheckFormDao;
import cc.comac.mom.util.BaseConnectionData;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.DataStatus;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;

@Component
@Transactional
public class QualityAsrCheckFormDataHandler extends ServiceImpl<QualityAsrCheckFormDao, QualityAsrCheckFormEntity> {

	@Autowired
	private QualityAsrCheckFormDao qualityAsrCheckFormDao;
	@Autowired
	private QualityQarCheckInvoiceDao qualityQarCheckInvoiceDao;
	@Autowired
	private QualityAsrCheckFormDetailDao qualityAsrCheckFormDetailDao;
	@Autowired
	private QualityQarBasicStandardAsrFeaturesDao qualityQarBasicStandardAsrFeaturesDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	@Autowired
	private QualityAsrCheckCorrigendumExplainDataHandler qualityAsrCheckCorrigendumExplainDataHandler;

	@Autowired
	private QualityAsrCheckFormDetailDataHandler qualityAsrCheckFormDetailDataHandler;
	@Autowired
	private QualityQarCheckInvoiceDataHandler qualityQarCheckInvoiceDataHandler;

	@Autowired
	private QualityRejectDataHandler qualityRejectDataHandler;

	@Autowired
	private QualityQarBasicStandardAsrFeaturesDataHandler qualityQarBasicStandardAsrFeaturesDataHandler;
	@Autowired
	private QualityQarBasicStandardDataHanldler qualityQarBasicStandardDataHanldler;
	@Autowired
	private BaseConnectionData baseConnectionData;

	// 保存修改按钮触发，保存表单数据以及对表单详情数据的增查改操作
	public ResultBean batchUpdateAsrCheckFormAndDetail(QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean resultBean = new ResultBean(true);
		QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
		if (innerData.getId() == 0) {
			throw new RuntimeException("无法获取表单主键！");
		}

		baseEntityDataUtil.updateBaseEntityData(innerData);
		// 更新表单数据
		qualityAsrCheckFormDao.updateById(innerData);
		// 增删改表单详情数据
		List<QualityAsrCheckFormDetail> formDetailEntityList = qualityAsrCheckForm.getFormDetailEntityList();
		if (formDetailEntityList.size() > 0) {
			qualityAsrCheckFormDetailDataHandler.batchInsertAsrCheckFormDetail(formDetailEntityList);
		}
		return resultBean;
	}

	// 提交检验按钮
	public ResultBean submitCheck(QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean resultBean = new ResultBean(true);
		QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
		if (StringUtils.isEmpty(innerData.getAsrNumber())) {
			throw new RuntimeException("无法获取检验表单序号");
		}
		// 查询检验标序号对应数据
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("asr_number", innerData.getAsrNumber());
		QualityAsrCheckFormEntity selectOne = qualityAsrCheckFormDao.selectOne(queryWrapper);
		if (!selectOne.getStatus().equals(DataStatus.planning)) {
			throw new RuntimeException("只有计划中状态才能提交检验！");
		}
		// 更新数据状态为检验中
		baseEntityDataUtil.updateBaseEntityData(selectOne);
		selectOne.setStatus(DataStatus.checking);
		selectOne.setQualityPlan(innerData.getQualityPlan());
		selectOne.setQualityPlanUser(innerData.getQualityPlanUser());
		selectOne.setQualityPlanTime(new Date());
		qualityAsrCheckFormDao.updateById(selectOne);
		return resultBean;
	}
	// 通过qar母本编码查询母本是否在升版中
	public void validateQarBasicStandardUpgrade (String QarBasicStandardNumber) {
		   // 通过qar编号获取母本数据
			QualityQarBasicStandard selectByQarBasicStandardNumber = this.selectByQarNumber(QarBasicStandardNumber);
			if (selectByQarBasicStandardNumber == null || selectByQarBasicStandardNumber.getInnerData() == null) {
				throw new RuntimeException("请核对当前检验单对应的母本信息");
			}
			if ("1".equals(selectByQarBasicStandardNumber.getInnerData().getStatus())) {
				throw new RuntimeException("当前母本在正在升版，无法提交审核！");
			}
	}

	// 提交审核
	public ResultBean submitReview(QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean resultBean = new ResultBean(true);
		QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
		if (StringUtils.isEmpty(innerData.getAsrNumber())) {
			throw new RuntimeException("无法获取检验表单序号");
		}
		// 检验母本是否在升版
		validateQarBasicStandardUpgrade(qualityAsrCheckForm.getInnerData().getQarNumber());
		// 查询检验标序号对应数据
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("asr_number", innerData.getAsrNumber());
		QualityAsrCheckFormEntity selectOne = qualityAsrCheckFormDao.selectOne(queryWrapper);
		if (!selectOne.getStatus().equals(DataStatus.checking)) {
			throw new RuntimeException("只有检验中状态才能提交审核！");
		}
		List<QualityAsrCheckFormDetailEntity> selectByFormId = qualityAsrCheckFormDetailDataHandler
				.selectByFormId(selectOne.getId());
		for (QualityAsrCheckFormDetailEntity qualityAsrCheckFormDetailEntity : selectByFormId) {
			if (qualityAsrCheckFormDetailEntity.getSampleQuantity() != qualityAsrCheckFormDetailEntity
					.getAcceptQuantity()) {
				throw new RuntimeException("样本数量与接收数量不同审核失败！");
			}
		}
		// 更新数据状态为审核中
		baseEntityDataUtil.updateBaseEntityData(selectOne);
		selectOne.setStatus(DataStatus.examining);
		selectOne.setQualityCheck(innerData.getQualityCheck());
		selectOne.setQualityCheckUser(innerData.getQualityCheckUser());
		selectOne.setQualityCheckTime(new Date());
		qualityAsrCheckFormDao.updateById(selectOne);
		return resultBean;
	}

	// 驳回按钮
	public ResultBean submitReject(QualityReject QualityReject) {
		ResultBean resultBean = new ResultBean(true);
		QualityRejectEntity innerData = QualityReject.getInnerData();
		if (StringUtils.isEmpty(innerData.getFormNo())) {
			throw new RuntimeException("无法获取检验表单序号");
		}
		// 查询检验标序号对应数据
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("asr_number", innerData.getFormNo());
		QualityAsrCheckFormEntity selectOne = qualityAsrCheckFormDao.selectOne(queryWrapper);
		// 驳回前后的状态
		String rejectBefore = selectOne.getStatus();
		String rejectAfter = QualityReject.getInnerData().getRejectEndStatus();
		// 更改驳回状态
		if (selectOne.getStatus().equals(DataStatus.finish)) {
			throw new RuntimeException("归档状态无法驳回");
		} else if (selectOne.getStatus().equals(DataStatus.planning)) {
			throw new RuntimeException("计划中状态无法驳回");
		}
		// 记录驳回历史
		selectOne.setStatus(rejectAfter);
		QualityReject.getInnerData().setRejectBeforeStatus(rejectBefore);
		QualityReject.getInnerData().setRejectEndStatus(rejectAfter);
		QualityReject.getInnerData().setRejectTime(TimeUtil.getLocalDate());
		baseEntityDataUtil.setBaseEntityData(innerData);
		qualityRejectDataHandler.insert(QualityReject);

		// 更新检验表单状态
		baseEntityDataUtil.updateBaseEntityData(selectOne);
        selectOne.setQualityCheckTime(null);
        selectOne.setQualityCheckUser("");
        selectOne.setQualityCheck("");
		if("planning".equals(rejectAfter)){
            selectOne.setQualityPlan("");
            selectOne.setQualityPlanUser("");
            selectOne.setQualityPlanTime(null);
        }
		qualityAsrCheckFormDao.updateById(selectOne);
		return resultBean;
	}

	// 归档
	public ResultBean submitToFile(QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean resultBean = new ResultBean(true);
		QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
		if (StringUtils.isEmpty(innerData.getAsrNumber())) {
			throw new RuntimeException("无法获取检验表单序号！");
		}
		// 查询检验标序号对应数据
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("asr_number", innerData.getAsrNumber());
		QualityAsrCheckFormEntity selectOne = qualityAsrCheckFormDao.selectOne(queryWrapper);
		if (!selectOne.getStatus().equals(DataStatus.examining)) {
			throw new RuntimeException("只有审核中状态才能归档！");
		}

		// 更新数据状态为审核中
		baseEntityDataUtil.updateBaseEntityData(selectOne);
		selectOne.setStatus(DataStatus.finish);
		selectOne.setToFile(innerData.getToFile());
		selectOne.setToFileUser(innerData.getToFileUser());
		selectOne.setToFileTime(new Date());
		qualityAsrCheckFormDao.updateById(selectOne);
		return resultBean;

	}

	// 校验字段数据
	public void validateFormData(QualityAsrCheckFormEntity qualityAsrCheckFormEntity) {
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getAsrNumber())) {
			throw new RuntimeException("asr编号不能为空！");
		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getFoPoNumber())) {
			throw new RuntimeException("PO//FO号不能为空！");
		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getFormType())) {
			throw new RuntimeException("表单类型不能为空！");
		}
//		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getPackingNumber())) {//手填
//			throw new RuntimeException("装箱单号不能为空！");
//		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getPartNumber())) {
			throw new RuntimeException("材料/零件图号不能为空！");
		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getPartType())) {
			throw new RuntimeException("材料类型不能为空！");
		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getProductModel())) {
			throw new RuntimeException("产品型号不能为空！");
		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getQarNumber())) {
			throw new RuntimeException("QAR单号不能为空！");
		}
		if (StringUtils.isEmpty(qualityAsrCheckFormEntity.getSupplierWorkshop())) {
			throw new RuntimeException("供应商/车间号不能为空！");
		}
	}

	public ResultBean batchInsertAsrCheckForm(List<QualityAsrCheckForm> list) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityAsrCheckFormEntity> formList = new ArrayList<QualityAsrCheckFormEntity>();
		List<QualityAsrCheckFormDetailEntity> detail=new ArrayList<>();
		if (list.size() > 0) {
			for (QualityAsrCheckForm qualityAsrCheckForm : list) {
				QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
				innerData.setAsrNumber(innerData.getQarNumber() + "-ASR");
				// 预留校验
				// .......
				validateFormData(innerData);
				baseEntityDataUtil.setBaseEntityData(innerData);
				formList.add(innerData);
				// 通过qar编号获取母本数据
				QualityQarBasicStandard selectByQarBasicStandardNumber = this.selectByQarNumber(innerData.getQarNumber());
				if (selectByQarBasicStandardNumber.getInnerData() != null) {
					QualityQarBasicStandardEntity innerData2 = selectByQarBasicStandardNumber.getInnerData();
					// 当母本数据不为空时，通过母本查询特征库母本中间表
					List<QualityAsrFeaturesBean> selectByQarBasicStandardId = qualityQarBasicStandardAsrFeaturesDataHandler
							.selectByQarBasicStandardId(innerData2.getId());
					// 当特征库数据不为空时获取特征库主键集合用于查询特征库数据
					if (selectByQarBasicStandardId.size() > 0) {
						for (QualityAsrFeaturesBean qualityAsrFeaturesBean : selectByQarBasicStandardId) {
							QualityAsrFeaturesEntity innerData3 = qualityAsrFeaturesBean.getInnerData();
							QualityAsrCheckFormDetailEntity qualityAsrCheckFormDetailEntity = new QualityAsrCheckFormDetailEntity();
							qualityAsrCheckFormDetailEntity.setAsrFormId(innerData.getId());
							qualityAsrCheckFormDetailEntity.setFeatures(innerData3.getFeatures());
							qualityAsrCheckFormDetailEntity.setSize(innerData3.getSize());
							qualityAsrCheckFormDetailEntity.setPartNumber(innerData3.getPartNumber());
							qualityAsrCheckFormDetailEntity.setInitialReliabilityValue(innerData3.getInitialReliabilityValue());
							qualityAsrCheckFormDetailEntity.setGrade(innerData3.getGrade());
							qualityAsrCheckFormDetailEntity.setFeaturesCount(innerData3.getFeaturesCount());
							baseEntityDataUtil.setBaseEntityData(qualityAsrCheckFormDetailEntity);
							detail.add(qualityAsrCheckFormDetailEntity);
						}
					}
				}



			}
			saveBatch(formList, formList.size());
			qualityAsrCheckFormDetailDataHandler.saveBatch(detail);
		}
		return resultBean;
	}

	public ResultBean deleteAsrCheckForm(QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean resultBean = new ResultBean(true);
		QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
		if (innerData.getId() == 0) {
			throw new RuntimeException("无法获取当前数据");
		}
		QualityAsrCheckFormEntity qualityAsrCheckFormEntity = new QualityAsrCheckFormEntity();
		qualityAsrCheckFormEntity.setId(innerData.getId());
		qualityAsrCheckFormEntity.setRowStatus(0);
		qualityAsrCheckFormDao.updateById(qualityAsrCheckFormEntity);
		return resultBean;
	}

	public ResultBean selectBySomefields(QualityAsrCheckForm qualityAsrCheckForm) {
		ResultBean resultBean = new ResultBean(true);
		if (qualityAsrCheckForm.getPageNum() == 0) {
			qualityAsrCheckForm.setPageNum(1);
		}
		if (qualityAsrCheckForm.getPageSize() == 0) {
			qualityAsrCheckForm.setPageSize(10);
		}
		
		QualityAsrCheckFormEntity innerData = qualityAsrCheckForm.getInnerData();
		// 通过材料零件图号获取分页数据
		Page<QualityAsrCheckFormEntity> page = new Page<QualityAsrCheckFormEntity>(qualityAsrCheckForm.getPageNum(),
				qualityAsrCheckForm.getPageSize(), true);
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		if (StringUtils.isNotEmpty(innerData.getAsrNumber())) {
			queryWrapper.eq("asr_number", innerData.getAsrNumber());
		}
		IPage<QualityAsrCheckFormEntity> selectPage = qualityAsrCheckFormDao.selectPage(page, queryWrapper);
		List<QualityAsrCheckFormEntity> records = selectPage.getRecords();
		
		// 组合数据
		List<QualityAsrCheckFormBean> qualityAsrCheckFormBeanList = new ArrayList<QualityAsrCheckFormBean>();
		for (QualityAsrCheckFormEntity qualityAsrCheckFormEntity : records) {
			QualityAsrCheckFormBean qualityAsrCheckFormBean = new QualityAsrCheckFormBean();
			copyToTargetBean(qualityAsrCheckFormEntity, qualityAsrCheckFormBean);
			// 将勘误数据组合到数据中
			List<QualityAsrCheckCorrigendumExplainEntity> selectByFormId = qualityAsrCheckCorrigendumExplainDataHandler
					.selectByFormId(qualityAsrCheckFormEntity.getId());
			if (selectByFormId.size() > 0) {
				qualityAsrCheckFormBean.setCorrigendumExplainList(selectByFormId);
			}

			// 将表单详情的数据组合到数据中心
			// 检验特征库数据
			List<QualityAsrCheckFormDetailEntity> selectByFormId2 = new ArrayList<QualityAsrCheckFormDetailEntity>();
			// 当是初始化数据时
			/*if (innerData.getId() == 0) {
				//先查检验单
				QualityQarCheckInvoiceEntity invoice=qualityQarCheckInvoiceDataHandler.selectByQarNumber(innerData.getQarNumber());
				// 通过qar编号获取母本数据
				if(invoice!=null) {
					QualityQarBasicStandard selectByQarBasicStandardNumber = qualityQarBasicStandardDataHanldler
							.selectById(invoice.getQarBasicStandardId());
					if (selectByQarBasicStandardNumber.getInnerData() != null) {
						QualityQarBasicStandardEntity innerData2 = selectByQarBasicStandardNumber.getInnerData();
						// 当母本数据不为空时，通过母本查询特征库母本中间表
						List<QualityAsrFeaturesBean> selectByQarBasicStandardId = qualityQarBasicStandardAsrFeaturesDataHandler
								.selectByQarBasicStandardId(innerData2.getId());
						// 当特征库数据不为空时获取特征库主键集合用于查询特征库数据
						if (selectByQarBasicStandardId.size() > 0) {
							for (QualityAsrFeaturesBean qualityAsrFeaturesBean : selectByQarBasicStandardId) {
								QualityAsrFeaturesEntity innerData3 = qualityAsrFeaturesBean.getInnerData();
								QualityAsrCheckFormDetailEntity qualityAsrCheckFormDetailEntity = new QualityAsrCheckFormDetailEntity();
								qualityAsrCheckFormDetailEntity.setFeatures(innerData3.getFeatures());
								qualityAsrCheckFormDetailEntity.setSize(innerData3.getSize());
								qualityAsrCheckFormDetailEntity.setInitialReliabilityValue(innerData3.getInitialReliabilityValue());
								qualityAsrCheckFormDetailEntity.setGrade(innerData3.getGrade());
								qualityAsrCheckFormDetailEntity.setFeaturesCount(innerData3.getFeaturesCount());
								selectByFormId2.add(qualityAsrCheckFormDetailEntity);
							}
						}
					}
				}

			} else {*/
				selectByFormId2 = qualityAsrCheckFormDetailDataHandler
						.selectByFormId(qualityAsrCheckFormEntity.getId());
			//}
			
			if (selectByFormId2.size() > 0) {
				qualityAsrCheckFormBean.setQualityAsrCheckFormDetailList(selectByFormId2);
			}

			qualityAsrCheckFormBeanList.add(qualityAsrCheckFormBean);
		}

		LimitPageBean<QualityAsrCheckFormBean> limitPageBean = new LimitPageBean<QualityAsrCheckFormBean>();
		limitPageBean.setEntityList(qualityAsrCheckFormBeanList);
		limitPageBean.setPageCount((int) Math.ceil(page.getTotal() * 1.0d / page.getSize()));
		limitPageBean.setPageNum((int) page.getCurrent());
		limitPageBean.setPageSize((int) page.getSize());
		limitPageBean.setTotalQuantity((int) page.getTotal());
		resultBean.setObject(limitPageBean);
		return resultBean;
	}

	// 将entity对象的值赋值给bean分支
	public void copyToTargetBean(QualityAsrCheckFormEntity qualityAsrCheckFormEntity,
			QualityAsrCheckFormBean qualityAsrCheckFormBean) {
		qualityAsrCheckFormBean.setAsrNumber(qualityAsrCheckFormEntity.getAsrNumber());
		qualityAsrCheckFormBean.setCheckRemark(qualityAsrCheckFormEntity.getCheckRemark());
		qualityAsrCheckFormBean.setCreationTime(qualityAsrCheckFormEntity.getCreationTime());
		qualityAsrCheckFormBean.setCreator(qualityAsrCheckFormEntity.getCreator());
		qualityAsrCheckFormBean.setFoPoNumber(qualityAsrCheckFormEntity.getFoPoNumber());
		qualityAsrCheckFormBean.setFormType(qualityAsrCheckFormEntity.getFormType());
		qualityAsrCheckFormBean.setPackingNumber(qualityAsrCheckFormEntity.getPackingNumber());
		qualityAsrCheckFormBean.setPartNumber(qualityAsrCheckFormEntity.getPartNumber());
		qualityAsrCheckFormBean.setPartType(qualityAsrCheckFormEntity.getPartType());
		qualityAsrCheckFormBean.setPlanRemark(qualityAsrCheckFormEntity.getPlanRemark());
		qualityAsrCheckFormBean.setProductModel(qualityAsrCheckFormEntity.getProductModel());
		qualityAsrCheckFormBean.setQarNumber(qualityAsrCheckFormEntity.getQarNumber());
		qualityAsrCheckFormBean.setQualityPlan(qualityAsrCheckFormEntity.getQualityPlan());
		qualityAsrCheckFormBean.setQualityPlanUser(qualityAsrCheckFormEntity.getQualityPlanUser());
		qualityAsrCheckFormBean.setQualityPlanTime(qualityAsrCheckFormEntity.getQualityPlanTime());
		qualityAsrCheckFormBean.setRowStatus(qualityAsrCheckFormEntity.getRowStatus());
		qualityAsrCheckFormBean.setStatus(qualityAsrCheckFormEntity.getStatus());
		qualityAsrCheckFormBean.setFreeze(qualityAsrCheckFormEntity.getFreeze());
		qualityAsrCheckFormBean.setSupplierWorkshop(qualityAsrCheckFormEntity.getSupplierWorkshop());
		qualityAsrCheckFormBean.setToFile(qualityAsrCheckFormEntity.getToFile());
		qualityAsrCheckFormBean.setToFileUser(qualityAsrCheckFormEntity.getToFileUser());
		qualityAsrCheckFormBean.setToFileTime(qualityAsrCheckFormEntity.getToFileTime());
		qualityAsrCheckFormBean.setQualityCheck(qualityAsrCheckFormEntity.getQualityCheck());
		qualityAsrCheckFormBean.setQualityCheckUser(qualityAsrCheckFormEntity.getQualityCheckUser());
		qualityAsrCheckFormBean.setQualityCheckTime(qualityAsrCheckFormEntity.getQualityCheckTime());
		qualityAsrCheckFormBean.setId(qualityAsrCheckFormEntity.getId());
	}

	// 通过检验表单中零件号查询检验数据
	public QualityAsrCheckFormEntity selectByPartNumber(String string) {
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("part_number", string);
		QualityAsrCheckFormEntity selectOne = qualityAsrCheckFormDao.selectOne(queryWrapper);
		return selectOne;
	}
	
	
	
// 通过
	public List<QualityAsrCheckFormBean> selectByCheckFormId(Long checkFormId) {
		// 通过材料零件图号获取分页数据
		QueryWrapper<QualityAsrCheckFormEntity> queryWrapper = new QueryWrapper<QualityAsrCheckFormEntity>();
		queryWrapper.eq("row_status", 1);
		if (checkFormId != 0) {
			queryWrapper.eq("asr_from_id", checkFormId);
		} else {
			throw new RuntimeException("检验表单主键为空，查询失败！");
		}
		List<QualityAsrCheckFormEntity> records = qualityAsrCheckFormDao.selectList(queryWrapper);
		
		// 组合数据
		List<QualityAsrCheckFormBean> qualityAsrCheckFormBeanList = new ArrayList<QualityAsrCheckFormBean>();
		for (QualityAsrCheckFormEntity qualityAsrCheckFormEntity : records) {
			QualityAsrCheckFormBean qualityAsrCheckFormBean = new QualityAsrCheckFormBean();
			copyToTargetBean(qualityAsrCheckFormEntity, qualityAsrCheckFormBean);
			// 将勘误数据组合到数据中
			List<QualityAsrCheckCorrigendumExplainEntity> selectByFormId = qualityAsrCheckCorrigendumExplainDataHandler
					.selectByFormId(qualityAsrCheckFormEntity.getId());
			if (selectByFormId.size() > 0) {
				qualityAsrCheckFormBean.setCorrigendumExplainList(selectByFormId);
			}

			// 将表单详情的数据组合到数据中心
			// 检验特征库数据
			List<QualityAsrCheckFormDetailEntity> selectByFormId2 = new ArrayList<QualityAsrCheckFormDetailEntity>();
			// 当是初始化数据时
				selectByFormId2 = qualityAsrCheckFormDetailDataHandler
						.selectByFormId(qualityAsrCheckFormEntity.getId());
			
			if (selectByFormId2.size() > 0) {
				qualityAsrCheckFormBean.setQualityAsrCheckFormDetailList(selectByFormId2);
			}

			qualityAsrCheckFormBeanList.add(qualityAsrCheckFormBean);
		}
		return qualityAsrCheckFormBeanList;
	}
	
	
	public AsrCheckFormPrint printByCheckformId(long formId) throws Exception {
		List<QualityAsrCheckFormBean> selectByCheckFormId = selectByCheckFormId(formId);
		AsrCheckFormPrint asrCheckFormPrint = new AsrCheckFormPrint();
		if (selectByCheckFormId.size() == 1) {
			QualityAsrCheckFormBean qualityAsrCheckFormBean = selectByCheckFormId.get(0);
			asrCheckFormPrint.setAsrFormId(qualityAsrCheckFormBean.getId());
			asrCheckFormPrint.setAsrNumber(qualityAsrCheckFormBean.getAsrNumber());
			asrCheckFormPrint.setAsrStatus(qualityAsrCheckFormBean.getStatus());
			asrCheckFormPrint.setCheckRemark(qualityAsrCheckFormBean.getCheckRemark());
			asrCheckFormPrint.setFoPoNumber(qualityAsrCheckFormBean.getFoPoNumber());
			asrCheckFormPrint.setQualityPlan(qualityAsrCheckFormBean.getQualityPlan());
			asrCheckFormPrint.setQualityCheck(qualityAsrCheckFormBean.getQualityCheck());
			asrCheckFormPrint.setToFile(qualityAsrCheckFormBean.getToFile());
			List<QualityAsrCheckCorrigendumExplainEntity> corrigendumExplainList = qualityAsrCheckFormBean.getCorrigendumExplainList();
			StringBuilder corrigendumExplain = new StringBuilder();
			for (QualityAsrCheckCorrigendumExplainEntity qualityAsrCheckCorrigendumExplainEntity : corrigendumExplainList) {
				String corrigendumName = qualityAsrCheckCorrigendumExplainEntity.getCorrigendumName();
				String corrigendumUser = qualityAsrCheckCorrigendumExplainEntity.getCorrigendumUser();
				String corrigendumUserStation = qualityAsrCheckCorrigendumExplainEntity.getCorrigendumUserStation();
				Date corrigendumTime = qualityAsrCheckCorrigendumExplainEntity.getCorrigendumTime();
				String fomatDate = TimeUtil.fomatDate(corrigendumTime);
				corrigendumExplain.append(" ").append(corrigendumName).append(" ");
				corrigendumExplain.append(corrigendumUserStation).append(" ");
				corrigendumExplain.append(corrigendumUser);
				corrigendumExplain.append("上传于").append(fomatDate);
			}
			asrCheckFormPrint.setCorrigendumExplain(corrigendumExplain.toString());
			asrCheckFormPrint.setPackingNo(qualityAsrCheckFormBean.getPackingNumber());
			asrCheckFormPrint.setPartNo(qualityAsrCheckFormBean.getPartNumber());
			asrCheckFormPrint.setPlanRemark(qualityAsrCheckFormBean.getPlanRemark());
			asrCheckFormPrint.setQarNumber(qualityAsrCheckFormBean.getQarNumber());
			asrCheckFormPrint.setShopNo(qualityAsrCheckFormBean.getSupplierWorkshop());
//			try {
////			Map<String, Object> beanToMap = beanToMap(asrCheckFormPrint);
////			Connection connection = baseConnectionData.getConnection();
//////			JasperRunManager.runReportToPdfFile("src/main/resources/config/asrCheckForm.jasper", "F:\\Download\\asrCheckForm.pdf", beanToMap,connection);
////			FileInputStream fileInputStream = new FileInputStream(new File("src/main/resources/config/asrCheckForm.jasper"));
////			runReportToPdf = JasperRunManager.runReportToPdf(fileInputStream, beanToMap, connection);
//			} catch (JRException e) {
//			e.printStackTrace();
//			}
		}
		return asrCheckFormPrint;
	}
	
	public  <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

	// 通过qar编号查询qar检验表信息
	public QualityQarBasicStandard selectByQarNumber(String qarNumber) {
		Long id= qualityQarCheckInvoiceDao.selectQarBasicStandardIdByQarNo(qarNumber);
		QualityQarBasicStandard selectByQarBasicStandardNumber = qualityQarBasicStandardDataHanldler
				.selectById(id);
		return selectByQarBasicStandardNumber;
	}

	//根据母本信息 同步全部的asr检验单详细数据
	public void batchsyscAsrCheckFormDetailInfo(List<Long> ids,List<QualityQarBasicStandardAsrFeatures> beanList,Long standardId) {

		//先删除检验单的详细数据
		qualityAsrCheckFormDetailDao.updateQualityAsrCheckFormDetail(ids);
		List<QualityAsrCheckFormDetailEntity> detail=new ArrayList<>();
		if (ids!=null&&ids.size()>0&&beanList!=null&&beanList.size()>0) {
			List<QualityAsrFeaturesBean> fInfo=
					qualityQarBasicStandardAsrFeaturesDataHandler.selectByQarBasicStandardId(standardId);
			for (Long id : ids) {
				for (QualityAsrFeaturesBean bean : fInfo) {
					QualityAsrFeaturesEntity innerData3 = bean.getInnerData();
					QualityAsrCheckFormDetailEntity qualityAsrCheckFormDetailEntity = new QualityAsrCheckFormDetailEntity();
					qualityAsrCheckFormDetailEntity.setAsrFormId(id);
					qualityAsrCheckFormDetailEntity.setFeatures(innerData3.getFeatures());
					qualityAsrCheckFormDetailEntity.setSize(innerData3.getSize());
					qualityAsrCheckFormDetailEntity.setPartNumber(innerData3.getPartNumber());
					qualityAsrCheckFormDetailEntity.setInitialReliabilityValue(innerData3.getInitialReliabilityValue());
					qualityAsrCheckFormDetailEntity.setGrade(innerData3.getGrade());
					qualityAsrCheckFormDetailEntity.setFeaturesCount(innerData3.getFeaturesCount());
					baseEntityDataUtil.setBaseEntityData(qualityAsrCheckFormDetailEntity);
					detail.add(qualityAsrCheckFormDetailEntity);
				}
			}
			qualityAsrCheckFormDetailDataHandler.saveBatch(detail);
		}
	}

	public List<QualityQarCheckInvoiceBean> selectQarCheckInvoiceInfo(Long qarBasicStandardId){
		return qualityAsrCheckFormDao.selectQarCheckInvoiceInfo(qarBasicStandardId);
	}

}
